from pymysql import connect, Error
from pymysql.cursors import DictCursor

# 用with语法改造
# with语法的好处就是打开了 可以自动关闭
try:
    # 建立连接
    conn = connect(
        host='127.0.0.1',
        port=3306,
        user='root',
        password='123456',
        database='my_news',
        # 将结果转换为字典
        cursorclass=DictCursor
    )
    with conn:
        with conn.cursor() as cursor:
            # 删除一条数据
            # id = 16
            # sql = 'DELETE FROM school_student_info WHERE id=%s'
            # cursor.execute(sql, (id,))

            # 删除多条数据
            dataList = ((14,), (13,))
            sql = 'DELETE FROM school_student_info WHERE id=%s'
            cursor.executemany(sql, dataList)
            conn.commit()
            print('执行完成')
except Error as e:
    print('连接失败：{}'.format(e))
